<?php

include_once $_SERVER['DOCUMENT_ROOT'].'HelmesWorkTest/backend/services/PersonSectorInvolvementService.php';
include_once $_SERVER['DOCUMENT_ROOT'].'HelmesWorkTest/backend/model/dao/mysqlimplementation/SectorDAOMySQLImpl.php';
include_once $_SERVER['DOCUMENT_ROOT'].'HelmesWorkTest/backend/model/dao/mysqlimplementation/PersonDAOMySQLImpl.php';
include_once $_SERVER['DOCUMENT_ROOT'].'HelmesWorkTest/backend/model/dao/mysqlimplementation/PersonSectorInvolvementDAOMySQLImpl.php';
include_once $_SERVER['DOCUMENT_ROOT'].'HelmesWorkTest/backend/database/ConnectionManager.php';
include_once $_SERVER['DOCUMENT_ROOT'].'HelmesWorkTest/GeneralConstants.php';
/**
 * Description of PersonSectorInvolvementServiceImpl
 *
 * @author Reinowin7
 */
class PersonSectorInvolvementServiceImpl implements PersonSectorInvolvementService
{
    
    public function getListOfAllSectors() 
    {
        $dbConnection = ConnectionManager::getInstance()->getConnection();
        $sectorDAOImpl = new SectorDAOMySQLImpl($dbConnection);
        $listOfAllSectors = null;
        
        try 
        {
            $listOfAllSectors = array();
            $listOfAllSectors = $sectorDAOImpl->getAllSectors();
        } 
        catch (Exception $e) 
        {
            ConnectionManager::getInstance()->closeConnection();
            $e->printStackTrace();
        }
        ConnectionManager::getInstance()->closeConnection();
		
        return $listOfAllSectors;
    }

    public function insertPersonWithSectorInvolvements($personSectorsInvolvementFormData)
    {
        $dbConnection = ConnectionManager::getInstance()->getConnection();
        $this->setAutocommitToFalse();
        $personDAOImpl = new PersonDAOMySQLImpl($dbConnection);
        $personSectorInvolvementDAOImpl = new PersonSectorInvolvementDAOMySQLImpl($dbConnection);
        $person = null;
        $listOfPersonSectorInvolvements = null;
        $listOfPotentialDatabaseInsertionErrors = array();
        $wasInsertionToADatabaseSuccessful = INSERTION_WAS_NOT_SUCCESSFUL;

        try 
        {
            $this->startTransaction();
            $person = $personSectorsInvolvementFormData->getPerson();
            array_push($listOfPotentialDatabaseInsertionErrors, $personDAOImpl->insertPerson($person));
            // Now, personID is set to a person object. It now can be used, to fill up the missing 
            //personFK field in PersonSectorInvolvement object
            $listOfPersonSectorInvolvements = $personSectorsInvolvementFormData->getListOfPersonSectorInvolvements();
            foreach ($listOfPersonSectorInvolvements as $personSectorInvolvement)
            {
                $personSectorInvolvement->setPersonFK($person->getPersonID());
                array_push($listOfPotentialDatabaseInsertionErrors, 
                        $personSectorInvolvementDAOImpl->insertPersonSectorInvolvement($personSectorInvolvement));
            }

            $wasInsertionToADatabaseSuccessful = $this->checkForPotentialErrors($listOfPotentialDatabaseInsertionErrors);

            if ($wasInsertionToADatabaseSuccessful == INSERTION_WAS_NOT_SUCCESSFUL)
            {
                $this->rollbackTransaction();
            }
            else
            {
                $this->commitTransaction();
            }
        }
        catch (Exception $e) 
        {
                $this->rollbackTransaction();
                $e->printStackTrace();
                return CODE_PRODUCED_AN_ERROR;
        }

        return $wasInsertionToADatabaseSuccessful;
    }

    public function updatePersonWithSectorInvolvements($personSectorsInvolvementFormData)
    {
        $dbConnection = ConnectionManager::getInstance()->getConnection();
        $this->setAutocommitToFalse();
        $personDAOImpl = new PersonDAOMySQLImpl($dbConnection);
        $personSectorInvolvementDAOImpl = new PersonSectorInvolvementDAOMySQLImpl($dbConnection);
        $person = null;
        $listOfPersonSectorInvolvements = null;
        $listOfPotentialDatabaseUpdateErrors = array();
        $wasUpdateInADatabaseSuccessful = UPDATE_WAS_NOT_SUCCESSFUL;

        try 
        {
            $this->startTransaction();
            $person = $personSectorsInvolvementFormData->getPerson();
            array_push($listOfPotentialDatabaseUpdateErrors, $personDAOImpl->updatePerson($person));
            // Now, personID is set to a person object. It now can be used, to fill up the missing 
            //personFK field in PersonSectorInvolvement object
            $listOfPersonSectorInvolvements = $personSectorsInvolvementFormData->getListOfPersonSectorInvolvements();
            //Lets delete previous relations before...
            array_push($listOfPotentialDatabaseUpdateErrors,
                    $personSectorInvolvementDAOImpl->deletePersonSectorInvolvement($person->getPersonID()));
            foreach ($listOfPersonSectorInvolvements as $personSectorInvolvement)
            {
                $personSectorInvolvement->setPersonFK($person->getPersonID());
                array_push($listOfPotentialDatabaseUpdateErrors, 
                $personSectorInvolvementDAOImpl->insertPersonSectorInvolvement($personSectorInvolvement));
            }

            $wasUpdateInADatabaseSuccessful = $this->checkForPotentialErrors($listOfPotentialDatabaseUpdateErrors);

            if ($wasUpdateInADatabaseSuccessful == UPDATE_WAS_NOT_SUCCESSFUL)
            {
                $this->rollbackTransaction();
            }
            else
            {
                $this->commitTransaction();
            }
        }
        catch (Exception $e) 
        {
            $this->rollbackTransaction();
            $e->getMessage();
            return CODE_PRODUCED_AN_ERROR;
        }
        
        return $wasUpdateInADatabaseSuccessful;
    }
    
    
    //---------------HELPER METHODS-----------------------
    private function setAutocommitToFalse()
    {
        mysql_query("SET AUTOCOMMIT=0");
    }
    
    private function startTransaction()
    {
        mysql_query("START TRANSACTION");
    }
    
    private function commitTransaction()
    {
        mysql_query("COMMIT");
    }
    
    private function rollbackTransaction()
    {
        mysql_query("ROLLBACK");
    }
    
    private function checkForPotentialErrors($listOfPotentialDatabaseInsertionErrors)
    {
        $ERROR_EXISTS = 0;
        $wasInsertionOrUpdateSuccessful = INSERTION_WAS_SUCCESSFUL;

        foreach ($listOfPotentialDatabaseInsertionErrors as $potentialError)
        {
            if ($potentialError == $ERROR_EXISTS)
            {
                $wasInsertionOrUpdateSuccessful = INSERTION_WAS_NOT_SUCCESSFUL;
                break;
            }
        }

        return $wasInsertionOrUpdateSuccessful;
    }
}

?>
